//
//  AppDelegate.swift
//  SplashMask
//
//  Created by tianqi on 2017/11/8.
//  Copyright © 2017年 david.com. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.main.bounds)
        let bgColor = UIColor(red: 0/255.0, green: 128/255.0, blue: 255/255.0, alpha: 1.0)
        self.window?.backgroundColor = bgColor
        self.window?.makeKeyAndVisible()
        

        let navVC = UIStoryboard(name: "Start", bundle: nil).instantiateViewController(withIdentifier: "NavgationVC") as! NavgationVC
        self.window?.rootViewController = navVC
        
        let view = navVC.view!
        
        let bgView = UIView(frame: view.bounds)
        bgView.backgroundColor = UIColor.white
        view.addSubview(bgView)
        view.bringSubview(toFront: bgView)
        
        
        let mask = CALayer()
        mask.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        mask.position = view.center
        mask.contents = #imageLiteral(resourceName: "logo").cgImage
        view.layer.mask = mask
        
        mask.presentation()
        let logoAnimation = CAKeyframeAnimation(keyPath: "bounds")
        logoAnimation.duration = 1.0
        logoAnimation.beginTime = CACurrentMediaTime() + 1.0
        logoAnimation.isRemovedOnCompletion = false
        logoAnimation.fillMode = kCAFillModeForwards
        let times = [NSNumber(value: 0),NSNumber(value: 0.5),NSNumber(value: 1)]
        logoAnimation.keyTimes = times
        let bounds1 = CGRect(x: 0, y: 0, width: 100, height: 100)
        let bounds2 = CGRect(x: 0, y: 0, width: 80, height: 80)
        let bounds3 = CGRect(x: 0, y: 0, width: 2000, height: 2000)
        logoAnimation.values = [bounds1,bounds2,bounds3]
        logoAnimation.timingFunctions = [CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut),
                                         CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut),
                                         CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)]
        view.layer.mask?.add(logoAnimation, forKey: "logoAnimation")
        
        UIView.animate(withDuration: 0.2, delay: 1.3, animations: {
            bgView.alpha = 0
        }) { (_) in
            bgView.removeFromSuperview()
        }
        
        UIView.animate(withDuration: 0.25, delay: 1.3, animations: {
            view.transform = view.transform.scaledBy(x: 1.1, y: 1.1)
            
        }) { (_) in
            UIView.animate(withDuration: 0.3, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
                view.transform = CGAffineTransform.identity
            }) { (_) in
                view.mask = nil
            }
        }
        
        
        return true
    }

}

